package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysPrizeEvent;
import com.ruoyi.system.domain.SysPrizeManagement;
import com.ruoyi.system.mapper.SysPrizeEventMapper;
import com.ruoyi.system.service.ISysPrizeEventService;
import com.ruoyi.system.service.ISysPrizeManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author HP
 * @Date 2021/2/26
 * @Description TODO
 */
@Service
public class SysPrizeEventServiceImpl implements ISysPrizeEventService {
    @Autowired
    private SysPrizeEventMapper sysPrizeEventMapper;
    @Autowired
    private ISysPrizeManagementService sysPrizeManagementService;
    /*
    添加赛事礼品
     */
    @Transactional
    @Override
    public void saveSysPrizeEvent(SysPrizeEvent sysPrizeEvent) throws Exception {
        if (sysPrizeEvent.getPid() == 0) {
            throw new Exception("礼品ID不能为空！");
        }
        SysPrizeManagement oldSPM = sysPrizeManagementService.selectById(sysPrizeEvent.getPid());
        if (oldSPM == null) {
            throw new Exception("当前礼品不存在！");
        }
        //总数量
        int totalCount = oldSPM.getCounts();
        //奖品数量
        int count = sysPrizeEvent.getPrizecount();
        //添加礼品设置
        if (sysPrizeEvent.getId() == null) {
            if (totalCount-count<0){
                throw new Exception("礼品库存不足！");
            }
            SysPrizeManagement sysPrizeManagement = new SysPrizeManagement();
            sysPrizeManagement.setCounts(count);
            sysPrizeManagement.setPid(sysPrizeEvent.getPid());
            //减礼品库存
            sysPrizeManagementService.updateByCounts1(sysPrizeManagement);
            sysPrizeEventMapper.saveSysPrizeEvent(sysPrizeEvent);
        } else { //修改礼品设置
            SysPrizeEvent spe = sysPrizeEventMapper.queryById(sysPrizeEvent.getId());
            int reduceCount = count - spe.getPrizecount();
            if (totalCount - reduceCount < 0) {
                throw new Exception("礼品库存不足！");
            }
            SysPrizeManagement sysPrizeManagement = new SysPrizeManagement();
            sysPrizeManagement.setCounts(reduceCount);
            sysPrizeManagement.setPid(sysPrizeEvent.getPid());
            //减礼品库存
            sysPrizeManagementService.updateByCounts1(sysPrizeManagement);
            //更新礼品设置
            sysPrizeEventMapper.updateSysPrizeEvent(sysPrizeEvent);
        }
    }
    /*
    查询所属赛事的礼品信息
     */
    @Override
    public List<SysPrizeEvent> querySysPrizeEvent(SysPrizeEvent sysPrizeEvent,Integer pageNum,Integer pageSize) {
        if (pageNum!=null&&pageSize!=null){
            PageHelper.startPage((pageNum / pageSize) + 1,pageSize);
        }
        List<SysPrizeEvent> sysPrizeEvents = sysPrizeEventMapper.querySysPrizeEvent(sysPrizeEvent);
        return sysPrizeEvents;
    }
    @Transactional
    @Override
    public void deleteSysPrizeEvent(int id) {
        SysPrizeEvent sysPrizeEvent = sysPrizeEventMapper.queryById(id);

        SysPrizeManagement sysPrizeManagement = new SysPrizeManagement();
        sysPrizeManagement.setPid(sysPrizeEvent.getPid());
        //恢复数量
        sysPrizeManagement.setCounts(- sysPrizeEvent.getPrizecount());
        //删除时，要恢复礼品总数量
        sysPrizeManagementService.updateByCounts1(sysPrizeManagement);
        sysPrizeEventMapper.deleteSysPrizeEvent(id);
    }

    @Override
    public SysPrizeEvent queryById(int id) {
        SysPrizeEvent sysPrizeEvent = sysPrizeEventMapper.queryById(id);
        return sysPrizeEvent;
    }

    @Override
    public List<SysPrizeEvent> queryByEpid(int epid) {
        return sysPrizeEventMapper.queryByEpid(epid);
    }

    @Override
    public void updateSysPrizeEvent(SysPrizeEvent sysPrizeEvent) {
        sysPrizeEventMapper.updateSysPrizeEvent(sysPrizeEvent);
    }

    @Override
    public void deletePid(int pid) {
        sysPrizeEventMapper.deletePid(pid);
    }


}
